import tkinter as tk
from tkinter import filedialog, messagebox
from tkinter import scrolledtext
from PIL import Image, ImageTk  # 用于图像处理

# 定义窗口类
class FishingApp:
    def __init__(self, root):
        self.root = root
        self.root.title("自动钓鱼应用")
        self.width = 600
        self.height = 600
        self.root.geometry(f"{self.width}x{self.height}")  # 设置窗口大小
        # 禁止调整大小
        self.root.resizable(False, False)

        # 定义变量
        self.image1_path = tk.StringVar()
        self.image2_path = tk.StringVar()
        self.image3_paths = []
        self.threshold1 = tk.DoubleVar()
        self.threshold2 = tk.DoubleVar()
        self.threshold3 = tk.DoubleVar()
        self.is_fishing = False
        self.grid_index=-1
        self.font_title=("Arial", 12, "bold")
        self.font_desc=("Arial", 10)
        self.font_input=("Arial", 10, "bold")

        # 创建控件
        self.create_widgets()
    def grid_index_next(self,next=True):
        if next:
            self.grid_index+=1
        return self.grid_index
        pass
    def create_widgets(self):
        # 检测图片1
        #创建label标签,单独一行,右对齐,距离顶部5px,距离左边20px,距离右边20px,下边距10px
        # 钓鱼准备工作:\n1.魔兽窗口最佳控制在1200,或者1500宽带左右 \n2. 第一人称视角最佳,\n3.脱出钓鱼技能放在当前可视位置可点击,\n4.自动钓鱼期间会控制鼠标移动右键
        help=tk.Label(self.root, text="钓鱼准备工作:\n1. 魔兽窗口最佳控制在1200,或者1500宽度左右 \n2. 第一人称视角最佳,\n3. 脱出钓鱼技能放在当前可视位置可点击,关闭UI缩放.让图标够大容易识别\n4. 自动钓鱼期间会控制鼠标移动右键", font=self.font_title, justify="left", wraplength=self.width)
        help.grid(row=self.grid_index_next(), column=0, columnspan=3, pady=5, sticky=tk.W)
        # tk.E (east) for right alignment
        # tk.N (north) for top alignment
        # tk.S (south) for bottom alignment
        # tk.NW (northwest) for top-left alignment
        # tk.NE (northeast) for top-right alignment
        # tk.SW (southwest) for bottom-left alignment
        # tk.SE (southeast) for bottom-right alignment
        tk.Label(self.root, text="钓鱼技能图片:").grid(row=self.grid_index_next(), column=0, sticky="w")
        # tk.Entry(self.root, textvariable=self.image1_path, width=40).grid(row=self.grid_index_next(False), column=1)
        # tk.Button(self.root, text="选择文件", command=self.select_image1).grid(row=self.grid_index_next(False), column=2)
        self.image1_preview = tk.Label(self.root,text="钓鱼图标",width=50, height=50,bg="white")
        self.image1_preview.grid(row=self.grid_index_next(False), column=1, columnspan=1,sticky="w")

        # # 检测阈值1
        # tk.Label(self.root, text="检测阈值1:").grid(row=self.grid_index_next(), column=0, sticky="e")
        # tk.Entry(self.root, textvariable=self.threshold1, width=10).grid(row=self.grid_index_next(), column=1)

        # # 检测图片2
        # tk.Label(self.root, text="检测图片2:").grid(row=self.grid_index_next(), column=0, sticky="e")
        # tk.Entry(self.root, textvariable=self.image2_path, width=40).grid(row=self.grid_index_next(), column=1)
        # tk.Button(self.root, text="选择文件", command=self.select_image2).grid(row=self.grid_index_next(), column=2)
        # self.image2_preview = tk.Label(self.root)
        # self.image2_preview.grid(row=self.grid_index_next(), column=0, columnspan=3, pady=5)

        # # 检测阈值2
        # tk.Label(self.root, text="检测阈值2:").grid(row=self.grid_index_next(), column=0, sticky="e")
        # tk.Entry(self.root, textvariable=self.threshold2, width=10).grid(row=self.grid_index_next(), column=1)

        # # 检测图片3 (支持多个)
        # tk.Label(self.root, text="检测图片3:").grid(row=self.grid_index_next(), column=0, sticky="e")
        # self.image3_listbox = tk.Listbox(self.root, height=5, width=40)
        # self.image3_listbox.grid(row=self.grid_index_next(), column=1, rowspan=3)
        # tk.Button(self.root, text="选择文件", command=self.select_images3).grid(row=self.grid_index_next(), column=2)
        # self.image3_preview = tk.Label(self.root)
        # self.image3_preview.grid(row=self.grid_index_next(), column=0, columnspan=3, pady=5)

        # # 检测阈值3
        # tk.Label(self.root, text="检测阈值3:").grid(row=self.grid_index_next(), column=0, sticky="e")
        # tk.Entry(self.root, textvariable=self.threshold3, width=10).grid(row=self.grid_index_next(), column=1)

        # # 保存按钮
        # tk.Button(self.root, text="保存", command=self.save_settings).grid(row=self.grid_index_next(), column=0, columnspan=2)

        # # 开始钓鱼按钮
        # self.fish_button = tk.Button(self.root, text="开始钓鱼", command=self.toggle_fishing)
        # self.fish_button.grid(row=self.grid_index_next(), column=0, columnspan=2)

        # 钓鱼日志信息（支持滚动）
        # self.log_label = tk.Label(self.root, text="钓鱼日志:")
        # self.log_label.grid(row=self.grid_index_next(), column=0, sticky="w")
        # self.log_text = scrolledtext.ScrolledText(self.root, width=60, height=10)
        # self.log_text.grid(row=self.grid_index_next(), column=0, columnspan=3)

    def select_image1(self):
        filename = filedialog.askopenfilename(filetypes=[("Image Files", "*.jpg *.png *.jpeg")])
        if filename:
            self.image1_path.set(filename)
            self.preview_image(filename, self.image1_preview)

    def select_image2(self):
        filename = filedialog.askopenfilename(filetypes=[("Image Files", "*.jpg *.png *.jpeg")])
        if filename:
            self.image2_path.set(filename)
            self.preview_image(filename, self.image2_preview)

    def select_images3(self):
        filenames = filedialog.askopenfilenames(filetypes=[("Image Files", "*.jpg *.png *.jpeg")])
        if filenames:
            self.image3_paths = filenames
            self.image3_listbox.delete(0, tk.END)
            for path in filenames:
                self.image3_listbox.insert(tk.END, path)
            if filenames:
                self.preview_image(filenames[0], self.image3_preview)

    def preview_image(self, image_path, label):
        """ 显示图像预览 """
        try:
            img = Image.open(image_path)
            img.thumbnail((150, 150))  # 缩放图片
            img = ImageTk.PhotoImage(img)
            label.config(image=img)
            label.image = img
        except Exception as e:
            messagebox.showerror("错误", f"无法加载图像: {e}")

    def save_settings(self):
        # 保存设置功能
        messagebox.showinfo("保存", "设置已保存!")

    def toggle_fishing(self):
        if self.is_fishing:
            self.is_fishing = False
            self.fish_button.config(text="开始钓鱼")
            self.log("停止钓鱼")
        else:
            self.is_fishing = True
            self.fish_button.config(text="停止钓鱼")
            self.log("开始钓鱼")

    def log(self, message):
        self.log_text.insert(tk.END, message + "\n")
        self.log_text.yview(tk.END)

# 创建窗口并运行应用
root = tk.Tk()
app = FishingApp(root)
root.mainloop()
